package com.tron.dobest.validation;

import com.tron.dobest.common.TronException;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@Api(tags = "参数校验")
@RestController
@RequestMapping("/rest")
//该注解，是为了让get请求校验有效。post请求不用加这个也能验证
@Validated
public class TronValidateController {

    @GetMapping("/validateTest01")
    public Object validateTest01(@RequestParam @Min(value = 1,message = "id最小值为1") String id) {
        return id;
    }

    /**
     * http://localhost:8080/rest/validateTest02?a
     * 只校验AddGroup.class中包含的校验集合，优先校验非Default.class属性
     * http://localhost:8080/rest/validateTest02?a=1&b=1,2&d=1&g=3,4
     * 没有加注解的值不会参与校验
     * @param validateData
     * @return
     */
    @GetMapping("/validateTest02")
    public Object validateTest02(@Validated(value = {AddGroup.class}) ValidateData validateData) {
        return validateData;
    }


    /**
     * 只校验UpdateGroup.class中包含的校验集合，优先校验非Default.class属性
     * 没有加注解的值不会参与校验
     *
    {
    "c":"111@qq.com",
    "g":[1,2],
    "d":3,
    "b":[3,4]
    }

     */
    @PostMapping("/validateTest03")
    public Object validateTest03(@RequestBody @Validated(value = UpdateGroup.class) ValidateData validateData) {
        return validateData;
    }



    /**
     * http://localhost:8080/rest/validateTest04?e=2020-01-01%2012:30:10
     * 只校验TronGroup.class中包含的校验集合
     */
    @GetMapping("/validateTest04")
    public Object validateTest04(@Validated(value = TronGroup.class) ValidateData validateData) {
        return validateData;
    }


    @GetMapping("/test1110")
    public Object test1110() {
        throw new TronException(400,"炮哥");
    }
}
